package com.wbao.dianniu.logical;

import android.content.Context;
import android.support.multidex.MultiDexApplication;

import com.alibaba.sdk.android.media.WantuService;
import com.alibaba.wxlib.util.SysUtil;
import com.baidu.mapapi.SDKInitializer;
import com.mob.MobSDK;
import com.tencent.imsdk.TIMGroupReceiveMessageOpt;
import com.tencent.imsdk.TIMManager;
import com.tencent.imsdk.TIMOfflinePushListener;
import com.tencent.imsdk.TIMOfflinePushNotification;
import com.tencent.qalsdk.sdk.MsfSdkUtils;
import com.tencent.qcloud.timchat.utils.Foreground;
import com.wbao.dianniu.R;
import com.wbao.dianniu.db.GreenDaoManager;
import com.wbao.dianniu.im.InitHelper;


public class DnApplication extends MultiDexApplication {
    //	private static final String TAG = DnApplication.class.getSimpleName();
//	 public static final String NAMESPACE = "openim";
    private static Context sContext;
    public static WantuService wantuService;

    public static Context getContext() {
        return sContext;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
//			initialize();
//            MultiDex.install(this);
    }

    /**
     *
     */
    @Override
    public void onCreate() {
        super.onCreate();
        // 注释移动热修复
        // queryAndLoadNewPatch不可放在attachBaseContext 中，否则无网络权限，建议放在后面任意时刻，如onCreate中
//		SophixManager.getInstance().queryAndLoadNewPatch();

        // 内存检测溢出初始化 dubug环境使用，线上版本屏蔽
//		LeakCanary.install(this);

        try {    // 增加try catch 在mustRunFirstInsideApplicationOnCreate初始化时会请求open log file 权限，此权限6.0以上系统可能被拒绝
            // todo
            // Application.onCreate中，首先执行这部分代码，以下代码固定在此处，不要改动，这里return是为了退出Application.onCreate！！！
            if (mustRunFirstInsideApplicationOnCreate()) {
                // todo 如果在":TCMSSevice"进程中，无需进行openIM和app业务的初始化，以节省内存
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 初始化云旺SDK
//		InitHelper.initYWSDK(this);

        sContext = getApplicationContext();
        initTencentOffline();

        // 顽兔初始化
        WantuService.enableHttpDNS(); // 可选。为了避免域名劫持，建议开发者启用HttpDNS
        WantuService.enableLog(); //在调试时，可以打开日志。正式上线前可以关闭
        wantuService = WantuService.getInstance();
        wantuService.asyncInit(this);

        if (MsfSdkUtils.isMainProcess(this)) {
            try {
                // 注意该方法要再setContentView方法之前实现 初始化 地图插件
                SDKInitializer.initialize(getApplicationContext());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        //GreenDao的初始化
        GreenDaoManager.getInstance();

    }

    private boolean mustRunFirstInsideApplicationOnCreate() {
        //必须的初始化
        SysUtil.setApplication(this);
        sContext = getApplicationContext();
        return SysUtil.isTCMSServiceProcess(sContext);
    }


    /**
     * 设置离线消息监听
     */
    private void initTencentOffline() {
        Foreground.init(this);
        if (MsfSdkUtils.isMainProcess(this)) {
            TIMManager.getInstance().setOfflinePushListener(new TIMOfflinePushListener() {
                @Override
                public void handleNotification(TIMOfflinePushNotification notification) {
                    if (notification.getGroupReceiveMsgOpt() == TIMGroupReceiveMessageOpt.ReceiveAndNotify) {
                        //消息被设置为需要提醒
                        notification.doNotify(getApplicationContext(), R.drawable.ic_launcher);
                    }
                }
            });
        }
    }


}
